import Vue from 'vue'
import VueRouter from 'vue-router'
import Register from '@/views/register'
import Layout from '@/views/layout'
/* 二级目录 */
import User from '@/views/user'
import Like from '@/views/like'
import Collect from '@/views/collect'
import Article from '@/views/article'
import Detail from '@/views/detail'
import { getToken } from '@/utils/storage'
const Login = () => import(/* webpackChunkName: "group-foo" */ '@/views/login.vue')

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    { path: '/register', component: Register },
    {
      path: '/',
      redirect: '/article',
      component: Layout,
      children: [
        { path: 'user', component: User },
        { path: 'like', component: Like },
        { path: 'collect', component: Collect },
        { path: 'article', component: Article }
      ]
    },
    { path: '/detail/:id', component: Detail }

  ]
})

const whiteList = ['/login', '/register']
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (whiteList.includes(to.path)) {
    next()
  } else if (token) {
    next()
  } else {
    next('/login')
  }
})

export default router
